<?php

/**
 * LoginForm class.
 * LoginForm is the data structure for keeping
 * user login form data. It is used by the 'login' action of 'SiteController'.
 */
class OrderForm extends CFormModel {

    public $receiptDate;
    public $receiptTime;
    public $productTotalPrice;
    public $deliverFee;
    public $note;
    public $paymentWay;
    public $orderState;
    public $totalPrice;
    public $userContactInfoId;
    public $sellerId;
    public $productSkuId;
    public $amount;
    public $province;
    public $city;
    public $district;
    public $streetDetail;
    public $mobile;
    public $area;
    public $street;
    public $recipients;
    public $postalcode;
    public $id;
    public $commont;
    public $productId;
    public $orderId;

    public function rules() {
        return array(
            array('receiptDate,receiptTime,productTotalPrice,deliverFee,paymentWay,orderState,userContactInfoId,amount,productSkuId,sellerId,id', 'required', 'on' => 'addUserOrder'),
            array('province,city,district,streetDetail,mobile,area,street,recipients,postalcode,note', 'safe'),
            array('commont,productId,orderId', 'safe')
        );
    }

    /**
     * Declares attribute labels.
     */
    public function attributeLabels() {
        return array(
            'recipients' => '收货人姓名',
            'area' => '所在地区',
            'postalcode' => '邮政编码',
            'telephone' => '座机号码',
            'road' => '街道地址',
            'streetDetail' => '详细地址',
            'mobile' => '手机号码',
            'oldpassword' => '旧密码',
            'newpassword' => '新密码',
            'newpassword_repeat' => '确认新密码',
            'receiptDate' => '收货日期',
            'receiptTime' => '收货时间',
            'commont' => '评论信息',
            'userContactInfoId' => '收货地址'
        );
    }

    public function addUserOrder() {

        $api = new Api('account');
//        $deliverFee = trim($this->deliverFee);
//        $note = trim($this->note);
//        $paymentWay = trim($this->paymentWay);
//        $productTotalPrice = trim($this->productTotalPrice);
//        $orderState = trim($this->orderState);
        $userContactInfoId = trim($this->userContactInfoId);

        $receiptDate = trim($this->receiptDate);
        $receiptTime = trim($this->receiptTime);

        $province = trim($this->province);
        $city = trim($this->city);
//        $district = trim($this->district);
        $streetDetail = trim($this->streetDetail);
        $mobile = trim($this->mobile);
        $area = trim($this->area);
        $street = trim($this->street);
        $recipients = trim($this->recipients);
        $postalcode = trim($this->postalcode);

        if (!$receiptDate || !$receiptTime) {
            Yii::app()->user->setFlash('message', '请选择完整的收货时间');
        } else {
            if ($userContactInfoId == 0) {
                $apiurl_addContact = "/LdAccountServer/order/addUserContactInfo";
                $params_addContact = array(
                    'AddUserContactInfoReq' => array(
                        'userId' => Yii::app()->user->id,
                        'province' => $province,
                        'city' => $city,
                        'area' => $area,
                        'street' => $street,
                        'streetDetail' => $streetDetail,
                        'postalcode' => $postalcode,
                        'recipients' => $recipients,
                        'mobile' => $mobile,
                        'defaultAddress' => 0,
                    )
                );
                $row_addContact = $api->apiclient($apiurl_addContact, $params_addContact);
                if ($row_addContact['code'] == 0) {
                    $userContactInfoId = $row_addContact['AddUserContactInfoRsp']['id'];
                    return $userContactInfoId;
                } else {
                    Yii::app()->user->setFlash('message', $row_addContact['ExceptionRsp']['msg']);
                    return false;
                }
            } else {
                return true;
            }
        }
        return false;
    }

    public function addCommont() {
        $api = new Api('account');
        $commont = $this->commont;
        $productId = $this->productId;
        $returns['code'] = 0;
        $returns['msg'] = '失败';
        if ($commont) {
            $apiurl = "/LdAccountServer/comment/addProductComment";
            $params = array(
                'AddProductCommentReq' => array(
                    'buyerId' => Yii::app()->user->id,
                    'content' => $commont,
                    'productIds' => $productId
                )
            );
//            print_r($params);
            $row = $api->apiclient($apiurl, $params);
            if ($row['code'] == 0) {
                foreach ($productId as $key => $value) {  // 增加商品评论数量+1
                    @file_get_contents("http://" . Yii::app()->params['apiHostProduct'] . ":" . Yii::app()->params['apiPortProduct'] . "/ProductCenter/UpdateProductBehaver/?productId=".$value."&type=3&productSkuId=");
                }
                $returns['code'] = 1;
                $returns['msg'] = '成功';
            } else {
                $returns['code'] = 0;
                $returns['msg'] = $row['ExceptionRsp']['msg'];
            }
        } else {
            $returns['code'] = -1;
            $returns['msg'] = '评论内容不能为空';
        }
        return $returns;
    }

}
